Parametricity, type equality, and higher-order polymorphism
نویسندگان
چکیده
Propositions that express type equality are a frequent ingredient of modern functional programming—they can encode generic functions, dynamic types, and GADTs. Via the Curry-Howard correspondence, these propositions are ordinary types inhabited by proof terms, computed using runtime type representations. In this paper we show that two examples of type equality propositions actually do reflect type equality; they are only inhabited when their arguments are equal and their proofs are unique (up to equivalence.) We show this result in the context of a strongly normalizing language with higher-order polymorphism and primitive recursion over runtime type representations by proving Reynolds’s abstraction theorem. We then use this theorem to derive “free” theorems about equality types. 1 Type equivalence, isomorphism and equality Type equivalence propositions assert that two types are isomorphic. For example, we may define such a proposition (in Haskell) as follows: type EQUIV a b = (a -> b, b -> a) Under the Curry-Howard correspondence, which identifies types and propositions, EQUIV asserts logical equivalence between two propositions: a implies b and b implies a. A proof of this equivalence, a pair of functions f and g, is a type isomorphism when the two functions compose to be the identity—in other words, when f . g = id and g . f = id. In that case, if (f,g) is a proof of the proposition EQUIV a Int, and x is an element of type a, then we can coerce x to be of type Int with f. In the past ten years, a number of authors have proposed the use of type equivalence propositions in typed programming languages (mostly Haskell). Type equivalence propositions have been used to implement heterogeneous data structures, type representations and generic functions, dynamic types, logical frameworks, metaprogramming, GADTs, and forms of lightweight dependent types (Yang, 1998; Weirich, 2004; Cheney & Hinze, 2002; Baars & Swierstra, 2002; Kiselyov etal., 2004; Chen etal., 2004; Sheard & Pasalic, 2004). 2 Dimitrios Vytiniotis and Stephanie Weirich Many of these authors point out that it is also possible to define a proposition that asserts that two types are not just equivalent, but that they are in fact equal. Equality is a stronger relation than equivalence as it must be substitutive as well as reflexive, symmetric and transitive [See (Kleene, 1967) page 157]. Type equality propositions are also called equality types. One definition of type equality is Leibniz equality—two types are equal iff one may be replaced with the other in all contexts. In Haskell, we may define the Leibniz equality proposition using higher-order polymorphism to quantify over all contexts. type EQUAL a b = forall c. c a -> c b Type equivalence and type equality propositions may be used for many of the same applications, but there are subtle differences between them. Equivalence holds for types that are not definitionally equal; for example, the types (Int, Bool) and (Bool, Int) are not equal in the Haskell type system, but they are isomorphic. One element of type EQUIV (Int, Bool) (Bool, Int) is two copies of a function that swaps the components of a pair. However, not all inhabitants of isomorphic types are type isomorphisms—for example, the term (const 0, const 1) inhabits the type EQUIV Int Int. Finally, some equivalent types are not isomorphic at all. For example, the proposition EQUIV Int Bool is provable, but not by any isomorphism between the types. In contrast, equality only holds for equal types and equal types are trivially isomorphic. There are no (terminating) inhabitants of type EQUAL Int Bool or of EQUAL (Int, Bool) (Bool, Int). We know this because of parametricity: for the latter type an inhabitant would need to know how to swap the components of the pair in an arbitrary context. Furthermore, the only inhabitants of type EQUAL Int Int are identity functions. Again, the reason is parametricity—because the context is abstract the function has no choice but to return its argument. These observations about the difference between the properties of type equivalence and of type equality are informal, and we would like to do better. In this paper, we make the previous arguments about type equality rigorous by deriving free theorems (Reynolds, 1983; Wadler, 1989) about equality types from Reynolds’s abstraction theorem. Reynolds’s abstraction theorem (also referred to as the “parametricity theorem” (Wadler, 1989) or the “fundamental theorem” of logical relations) asserts that every well-typed expression of the polymorphic λ-calculus (System F) (Girard, 1972) satisfies a property directly derivable from its type. We derive these free theorems from the parametricity theorem for a language called Rω (Crary etal., 2002), which extends Girard’s Fω with constructs that are useful for programming with type equivalence propositions (see the next section). Using these constructs in Rω we can define a type-safe cast operation which compares types and produces an equality proof when they are the same. This extension comes at little cost as the necessary modifications to the Fω parametricity theorem are modest and localized. Like Fω, Rω is a (provably, using the results in this paper) terminating language, which simplifies our development and allows us to focus on the parametricity properties of higher-order polymorphism. Of course, our results will not carry over to full languages like Haskell without extension. Parametricity, Type Equality and Higher-order Polymorphism 3 After proving a version of the abstraction theorem for Rω, we show how to apply it to the type EQUAL to show that it is inhabited only when the source and target types are the same, in which case that inhabitant must be the identity. Our use of free theorems for higher-order polymorphism exhibits an intriguing behavior. Whereas free theorems for second-order polymorphism quantify over arbitrary relations, they are often instantiated with (the graphs of) functions expressible in the polymorphic λ-calculus (Wadler, 1989). By contrast, in our examples we instantiate free theorems with (the graphs of) non-parametric functions.
منابع مشابه
Comprehensive Parametric Polymorphism: Categorical Models and Type Theory
This paper combines reflexive-graph-category structure for relational parametricity with fibrational models of impredicative polymorphism. To achieve this, we modify the definition of fibrational model of impredicative polymorphism by adding one further ingredient to the structure: comprehension in the sense of Lawvere. Our main result is that such comprehensive models, once further endowed wit...
متن کاملType-safe cast does no harm Theoretical Pearl
Generic functions can specialize their behaviour depending on the types of their arguments, and can even recurse over the structure of the types of their arguments. Such functions can be programmed using type representations. Generic functions programmed this way possess certain parametricity properties, which become interesting in the presence of higher-order polymorphism. In this Theoretical ...
متن کاملSyntax for Free: Representing Syntax with Binding Using Parametricity
We show that, in a parametric model of polymorphism, the type ∀α.((α → α) → α) → (α → α → α) → α is isomorphic to closed de Bruijn terms. That is, the type of closed higher-order abstract syntax terms is isomorphic to a concrete representation. To demonstrate the proof we have constructed a model of parametric polymorphism inside the Coq proof assistant. The proof of the theorem requires parame...
متن کاملSpeci cation Re nement with System
Essential concepts of algebraic speciication reenement are translated into a type-theoretic setting involving System F and Reynolds' relational parametricity assertion as expressed in Plotkin and Abadi's logic for parametric polymorphism. At rst order, the type-theoretic setting provides a canonical picture of algebraic speciication reenement. At higher order, the type-theoretic setting allows ...
متن کاملSemantics of Parametric Polymorphism in Imperative Programming Languages
Programming languages such as CLU, Ada and Modula-3 have included facilities for parametric polymorphism and, more recently, C++ and Java have also added similar facilities. In this paper, we examine the issues of defining denotational semantics for imperative programming languages with polymorphism. We use the framework of reflexive graphs of categories previously developed for a general axiom...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید
ثبت ناماگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید
ورودعنوان ژورنال:
- J. Funct. Program.
دوره 20 شماره
صفحات -
تاریخ انتشار 2010